#!/usr/bin/env bash

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

set -eu

WORK_DIR=$WORK_DIR/sasl_fed
mkdir $WORK_DIR

sasl_config_dir=$BUILD_DIR/src/tests/sasl_config

# Create ACL file to allow links
echo acl allow all all > $WORK_DIR/sasl_fed.acl

echo "Starting broker 1"

qpidd                                        \
  -p 0  --interface 127.0.0.1                \
  --data-dir $WORK_DIR/data_1                \
  --auth=yes                                 \
  --mgmt-enable=yes                          \
  --log-enable info+                         \
  --log-source yes                           \
  --log-to-file $WORK_DIR/qpidd_1.log        \
  --sasl-config=$sasl_config_dir             \
  --acl-file $WORK_DIR/sasl_fed.acl          \
  -d > $WORK_DIR/broker_1_port

broker_1_port=$(cat $WORK_DIR/broker_1_port)

echo "Starting broker 2"

qpidd                                        \
  -p 0 --interface 127.0.0.1                 \
  --data-dir $WORK_DIR/data_2                \
  --auth=yes                                 \
  --mgmt-enable=yes                          \
  --log-enable info+                         \
  --log-source yes                           \
  --log-to-file $WORK_DIR/qpidd_2.log        \
  --sasl-config=$sasl_config_dir             \
  --acl-file $WORK_DIR/sasl_fed.acl          \
  -d > $WORK_DIR/broker_2_port

broker_2_port=$(cat $WORK_DIR/broker_2_port)

function stop_brokers {
    qpidd --port $broker_1_port --quit
    qpidd --port $broker_2_port --quit
}

trap stop_brokers EXIT

sleep 2

# I am not randomizing these names, because the test creates its own brokers.
QUEUE_NAME=sasl_fed_queue
ROUTING_KEY=sasl_fed_queue
EXCHANGE_NAME=sasl_fedex

echo "Adding exchanges"

qpid-config -b localhost:$broker_1_port add exchange direct $EXCHANGE_NAME
qpid-config -b localhost:$broker_2_port add exchange direct $EXCHANGE_NAME

echo "Adding queues"

qpid-config -b localhost:$broker_1_port add queue $QUEUE_NAME
qpid-config -b localhost:$broker_2_port add queue $QUEUE_NAME

sleep 5

echo "Creating bindings"

qpid-config -b localhost:$broker_1_port bind $EXCHANGE_NAME $QUEUE_NAME $ROUTING_KEY
qpid-config -b localhost:$broker_2_port bind $EXCHANGE_NAME $QUEUE_NAME $ROUTING_KEY

sleep 5

echo "Adding routes"

qpid-route route add zag/zag@localhost:$broker_2_port zag/zag@localhost:$broker_1_port $EXCHANGE_NAME $ROUTING_KEY "" "" DIGEST-MD5

sleep 5

n_messages=100

echo "Sending 100 messages to $broker_1_port "

datagen --count $n_messages | sender --mechanism DIGEST-MD5 --username zag --password zag --exchange $EXCHANGE_NAME --routing-key $ROUTING_KEY --port $broker_1_port

sleep 5

echo "Examining Broker $broker_1_port"

broker_1_message_count=$(qpid-stat -q -b localhost:$broker_1_port | grep sasl_fed_queue | awk '{print $2}')

echo "Examining Broker $broker_2_port"

broker_2_message_count=$(qpid-stat -q -b localhost:$broker_2_port | grep sasl_fed_queue | awk '{print $2}')

if (( $broker_2_message_count != $n_messages )); then
    echo "Expected ${n_messages} and received ${broker_2_message_count}"
    exit 1
fi
